package com.changgou.gateway.filter;

import com.changgou.gateway.util.JwtUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Authorization;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 描述
 *
 * @author ljh
 * @version 1.0
 * @package com.changgou.gateway.filter *
 * @Date 2019-12-2
 * @since 1.0
 */
@Component
public class ChanggouGlobalFilter implements GlobalFilter, Ordered {
    private static final String AuthorizationHeaderName = "Authorization";

    //实现权限校验
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        //1.获取请求对象
        ServerHttpRequest request = exchange.getRequest();
        //2.获取响应对象
        ServerHttpResponse response = exchange.getResponse();
        //3.判断请求的url是否需要拦截(如果是登录,需要放行)
        String path = request.getURI().getPath();//       /user/login
        if (path.startsWith("/api/user/login")) {
            //放行
            return chain.filter(exchange);
        }
        //4.先从请求头中获取令牌信息
        String token = request.getHeaders().getFirst(AuthorizationHeaderName);
        if (StringUtils.isEmpty(token)) {
            //5.如果请求头中没有令牌  再从请求参数中获取
            token = request.getQueryParams().getFirst(AuthorizationHeaderName);
        }

        if (StringUtils.isEmpty(token)) {
            //6.如果请求参数中没有令牌 再从cookie中获取
            HttpCookie cookie = request.getCookies().getFirst(AuthorizationHeaderName);
            if (cookie != null) {
                token = cookie.getValue();
            }
        }
        if (StringUtils.isEmpty(token)) {
            //7.判断是否有令牌信息,如果 没有 直接返回(拒绝:你没有权限)
            // 如果用户没有携带token 说明没有登录,需要重定向到登录的页面
            response.setStatusCode(HttpStatus.SEE_OTHER);
            String s = request.getURI().toString();
            response.getHeaders().set("Location","http://localhost:9001/oauth/login?from="+s);
            return response.setComplete();
        }
        //8.如果 有令牌 进行解析 解析失败 返回,解析成功就放行
       /* try {
            Claims claims = JwtUtil.parseJWT(token);
            return chain.filter(exchange);
        } catch (Exception e) {
            e.printStackTrace();
            response.setStatusCode(HttpStatus.UNAUTHORIZED);//401
            return response.setComplete();
        }*/


        //获取到令牌信息 放到请求头中路由给微服务
        request.mutate().header(AuthorizationHeaderName, "bearer " + token);


        return chain.filter(exchange);

    }

    //设置过滤器执行的顺序
    @Override
    public int getOrder() {
        return 0;
    }
}
